package zhiweio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;

class Opponent {

    private String rival;

    public Opponent(String rival) {
        this.rival = rival;
    }

    public String guess() {
        final String[] choices = {"scissor", "stone", "fabric"};

        if (this.rival.equals("Reboot")) {
            Random random = new Random();
            int index = random.nextInt(choices.length);
            return choices[index];
        }
        else if (this.rival.equals("Human")) {
            Scanner input = new Scanner(System.in);
            System.out.println("guess one:");
            if (input.hasNext()) {
                return input.nextLine();
            }
        }
        else {
            System.out.print("You got no opponent");
        }

        return null;
    }
}


public class MorraGame {

    public static void main(String[] args) {

        System.out.println("Start Morra Game");
        String choose = chooseOpponent();
        Opponent opponent = new Opponent(choose);
        System.out.println("\n[scissor, stone, fabric]\n");
        estimate(opponent.guess(), guess());

    }

    private static String chooseOpponent() {
        final String[] opponents = {"Reboot", "Human"};

        System.out.println("Choose your opponent\n1: Reboot\n2: Human");
        System.out.println("Enter an index number (1 or 2):");

        try{
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            String input = stdin.readLine();

            try{
                int index = Integer.parseInt(input);
                System.out.println("Your opponent is: "+opponents[index - 1]);
                return opponents[index - 1];
            }
            catch(NumberFormatException e){
                System.out.println("Index should be an integer");
            }
            catch(IndexOutOfBoundsException e){
                System.out.println("Index must between 1 or 2");
            }
        }
        catch(IOException e){
            System.out.println(e);
        }
        return null;
    }

    private static String guess() {
        Scanner input = new Scanner(System.in);
        System.out.println("guess one:");
        if (input.hasNext()) {
            return input.nextLine();
        }
        return null;
    }

    private static void estimate(String first, String last) {
        final String[] lose = {"scissor->stone", "stone->fabric", "fabric->scissor"};
        final String[] win = {"stone->scissor", "fabric->stone", "scissor->fabric"};

       String morra = first+"->"+last;
       System.out.println("result: "+morra+"\n");

        if (first.equals(last)) {
            System.out.println("no winner");
        }

        for (String x: lose) {
            if (x.equals(morra)){
                System.out.println("Sorry, You lost!!");
            }
        }
        for (String x: win) {
            if (x.equals(morra)) {
                System.out.println("Congratulations, You won!!");
            }
        }
    }
}
